package com.cskaoyan.service;

import com.cskaoyan.bean.Brand;
import com.cskaoyan.bean.BrandExample;
import com.cskaoyan.mapper.BrandMapper;
import com.cskaoyan.mapper.StorageMapper;
import com.cskaoyan.vo.BrandVo;
import com.cskaoyan.vo.CreateBrandVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 *
 * @author chen11
 */

@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    BrandMapper brandMapper;
    @Autowired
    StorageMapper storageMapper;

    @Override
    public BrandVo selectBrandList(Integer page,Integer limit,Integer id,String name,String sort,String order) {
        PageHelper.startPage(page, limit);
        BrandExample brandExample = new BrandExample();
        brandExample.setOrderByClause(sort+" "+order);
        BrandExample.Criteria criteria = brandExample.createCriteria();

        if(name==null && id==null){
            criteria.andIdIsNotNull();
        }else if(name !=null && id==null){
            criteria.andNameEqualTo(name);
        }else if(name==null && id!=null){
            criteria.andIdEqualTo(id);
        }else {
            criteria.andNameEqualTo(name).andIdEqualTo(id);
        }


        List<Brand> brands = brandMapper.selectByExample(brandExample);
        PageInfo<Brand> brandPageInfo = new PageInfo<>(brands);
        int total = (int) brandPageInfo.getTotal();

        BrandVo brandVo = new BrandVo();
        brandVo.setTatal(total);
        brandVo.setItems(brands);

        return brandVo;

    }

    @Override
    public void deleteBrand(Integer id) {

        //删除storage中图片信息
        Brand brand = brandMapper.selectByPrimaryKey(id);
        String picUrl = brand.getPicUrl();


        //删除brand表中数据
        brandMapper.deleteByPrimaryKey(id);






    }

    @Override
    public Brand updateBrand(Brand brand) {

        brandMapper.updateByPrimaryKey(brand);
        Brand brand1 = brandMapper.selectByPrimaryKey(brand.getId());
        return brand1;
    }

    @Override
    public CreateBrandVo createBrand(Brand brand) {
        Date date = new Date();
        brand.setAddTime(date);
        brand.setUpdateTime(date);
        brand.setDeleted(false);
        brandMapper.insert(brand);
         //插入之后返回主键
         Integer id = brand.getId();

        Brand brand1 = brandMapper.selectByPrimaryKey(id);

        CreateBrandVo createBrandVo = new CreateBrandVo();
        createBrandVo.setId(brand1.getId());
        createBrandVo.setName(brand1.getName());
        createBrandVo.setDesc(brand1.getDesc());
        createBrandVo.setPicUrl(brand1.getPicUrl());
        createBrandVo.setFloorPrice(brand1.getFloorPrice());
        createBrandVo.setAddTime(brand1.getAddTime());
        createBrandVo.setUpdateTime(brand1.getUpdateTime());

        return createBrandVo;

    }
    public List<Brand> queryAllByPage(Integer page, Integer size) {
        BrandExample example = new BrandExample();
        example.or().andDeletedEqualTo(false);
        example.setOrderByClause("add_time desc");
        PageHelper.startPage(page,size);
        return brandMapper.selectByExample(example);
    }

    public Brand selectById(Integer id) {
        return brandMapper.selectByPrimaryKey(id);

    }


}
